
package com.cory.admin.sys.service;

import java.util.List;

import com.cory.boot.domain.bean.Principal;
import com.cory.boot.utils.SysUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cory.admin.sys.domain.entity.SysRole;
import com.cory.admin.sys.mapper.SysRoleMapper;
import com.cory.boot.domain.enumdef.EState;
import com.cory.module.oplog.core.OpLogBaseService;

/**
 *  服务实现类
 *
 * @author xc
 * @since 2019-02-23
 */
@Service
public class SysRoleService extends OpLogBaseService<SysRoleMapper, SysRole> {

    public List<SysRole> list(SysRole param) {
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.eq("tenant_id", param.getTenantId()).eq("user_type", param.getUserType()).ne("state", EState.DELETE.key);
        query.orderByDesc("id");
        if (param.getUserType() != null) {
            query.eq("user_type", param.getUserType());
        }
        if (StringUtils.isNotBlank(param.getName())) {
            query.like("name", "%" + param.getName() + "%");
        }
        if (param.getState() != null) {
            query.eq("state", param.getState());
        }
        return super.list(query);
    }


    /**
     * 获取用户对应的角色列表
     * @param userId
     * @return
     */
    public List<SysRole> selectRoleListByUserId(Integer userId) {
        return baseMapper.selectListByUserId(userId);
    }

    /**
     * 获取租户下的角色列表
     * @return
     */
    public List<SysRole> selectRoleList() {
        Principal user = SysUtils.getPrincipal();
        return baseMapper.selectByTenantId(user.getTenantId());
    }
    
}
